import {Component, OnInit} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { ForumService } from '../../service/ForumService';
import { AuthService } from '../../service/AuthService';
import { Forum } from '../../models/Forum';
import { Comment } from '../../models/Comment';
import { ForumDetailService } from '../../service/ForumDetailService';
import { User } from '../../models/User';
import { CommentZan } from '../../models/CommentZan';
import {CommentReply} from '../../models/CommentReply';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  templateUrl: './forumDetail.html',
  styleUrls: ['./forumDetail.css']
})

export class ForumDetailComponent implements OnInit {
  forum: Forum;
  id: number;
  zanCount: number;
  commentCount: number;
  comments: Comment[];
  comment: Comment;
  user: User;
  commentZan: CommentZan;
  judge: boolean;
  reply: CommentReply;
  forumId: number;
  selectComment: Comment;
  constructor(
    private route: ActivatedRoute,
    private forumservice: ForumService,
    private location: Location,
    private  forumDetailService: ForumDetailService,
    private  authService: AuthService,
    private sanitizer: DomSanitizer
  ) {}
  ngOnInit() {
    this.forum = {};
    this.commentZan = {};
    this.comment = {};
    this.reply = {};
    this.judge = true;
    this.getForum();
    this.getCount();
    this.getComments();
    this.user = this.authService.user;
    this.authService.userObservable.subscribe((user: User) => {
      this.user = user;
    });
    this.forumId = +this.route.snapshot.paramMap.get('id');
  }
  getForum() {
    this.id = +this.route.snapshot.paramMap.get('id');
    this.forumservice.getForum(this.id).subscribe((res) => {
      this.forum = res.res;
      // this.forum.content = this.reformContent(this.forum.content);
      // this.forum.content = this.sanitizer.bypassSecurityTrustHtml(this.forum.content as string);
    });
  }
  goBack(): void {
    this.location.back();
  }
  // reformContent(content) {
  //   content = content.split('');
  //   this.tagBoolean = false;
  //   content.forEach((c, index) => {
  //     if ('<' === c) {
  //       this.tagBoolean = true;
  //     } else if ('>' === c) {
  //       content[index] = '';
  //       this.tagBoolean = false;
  //     }
  //     if (this.tagBoolean) {
  //       content[index] = '';
  //     }
  //   });
  //   content = content.join('');
  //   return content;
  // }
  getCount() {
    this.forumDetailService.getNewsCounts(this.id).subscribe((res) => {
      this.zanCount = res.res;
    })
    this.forumDetailService.getCount(this.id).subscribe((res) => {
      this.commentCount = res.res;
    })
  }
  getComments() {
    this.forumDetailService.getComments(Number(''), Number(''), String(this.id)).subscribe((res) => {
      this.comments = res.res;
    })
  }
  addComment() {
      this.comment.news_id = this.id;
      this.comment.from_uid = Number(this.user.id);
      this.comment.sysUser = this.user;
      this.forumDetailService.insertComment(this.comment).subscribe( (res) => {
        this.commentCount += 1;
        this.comments.push(res.res);
      });
  }
  addZan() {
    this.commentZan.sysUser = this.user;
    this.commentZan.to_uid = this.forumId;
    this.commentZan.type_id = this.id;
    this.commentZan.type = 1;
    if (this.judge) {
      this.forumDetailService.insertZan(this.commentZan).subscribe((res) => {
        this.judge = false;
        this.commentZan.id = res.res.id;
        this.zanCount += 1;
      })
  } else {
      this.forumDetailService.updateZan(this.commentZan).subscribe((res) => {
        if (res.success) {
          this.judge = true;
          this.zanCount -= 1;
        }
      })
    }
  }
  getReply(commentId, comment) {
    this.forumDetailService.getReply(commentId).subscribe((res) => {
      comment.replys = res.res;
      if (res.res.length !== 0) {
        comment.selectComment = true;
      } else {
        alert('还没有评论，快来抢沙发吧');
      }
    })
  }
 changeReply(comment) {
    comment.replayValue = true;
  }
  hideReply(comment) {
    comment.selectComment = false;
  }
  insertReply(comment) {
    this.reply.comment_id = comment.id;
    this.reply.from_uid = Number(this.user.id);
    this.reply.to_uid = comment.from_uid;
    this.reply.content = comment.reply;
    this.forumDetailService.insertReply(this.reply).subscribe((res) => {
      // comment.selectComment = true;
      // comment.replys.push(res.res);
    })
    comment.replayValue = false;
  }
  deleteReply(commentId) {
    this.forumDetailService.deleteReply(commentId).subscribe((res) => {});
  }
  deleteComment(commentId) {
    this.forumDetailService.deleteComment(commentId).subscribe((res) => {
      if (res.success) {
        alert( '删除成功！' );
        this.getComments();
      }
    });
  }
}
